package cn.hg.solon.youcan.web.admin.controller.monitor;

import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.text.CharSequenceUtil;
import org.dromara.hutool.core.text.StrValidator;
import org.dromara.hutool.core.text.split.SplitUtil;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.core.handle.Result;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.NotNull;

import com.mybatisflex.core.paginate.Page;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hg.solon.youcan.common.annotation.Log;
import cn.hg.solon.youcan.common.enums.BusinessType;
import cn.hg.solon.youcan.common.web.LayuiPage;
import cn.hg.solon.youcan.system.entity.SysOperateLog;
import cn.hg.solon.youcan.system.service.SysOperateLogService;
import cn.hg.solon.youcan.web.admin.controller.BaseAdminController;
import cn.hg.solon.youcan.web.admin.controller.monitor.qo.OperateLogQueryObject;

/**
 * @author 胡高
 */
@Controller
@Mapping("/admin/monitor/operate")
public class OperateLogController extends BaseAdminController {

    private static final String SERVICE_NAME = "操作日志";

    private static String VIEW_PATH = "/admin/monitor/operate/";

    @Inject
    SysOperateLogService operateLogService;

    @Mapping(path = "clear", method = MethodType.POST)
    @Log(title = SERVICE_NAME, businessType = BusinessType.CLEAR)
    @SaCheckPermission(value = {"monitor:operate:clear"})
    public Result<?> clear(Context ctx, OperateLogQueryObject query) {
        this.log.info("清除所有[{}]记录", SERVICE_NAME);

        this.operateLogService.removeAll();

        return Result.succeed();
    }

    @Mapping(path = "delete", method = MethodType.DELETE)
    @Log(title = SERVICE_NAME, businessType = BusinessType.DELETE)
    @SaCheckPermission(value = {"monitor:operate:del"})
    public Result<?> delete(Context ctx, @NotBlank String ids) {
        this.log.info("删除[{}]记录：ids={}", SERVICE_NAME, ids);

        return this.operateLogService
            .removeByIds(StrValidator.isBlank(ids) ? ListUtil.empty() : SplitUtil.split(ids, ",")) ? Result.succeed()
                : Result.failure();
    }

    /**
     * 跳转到记录展示页面
     */
    @Mapping(path = "detail", method = MethodType.GET)
    @SaCheckPermission(value = {"monitor:operate:query"})
    public ModelAndView detail(Context ctx, @NotNull Integer id) {
        this.log.info("跳转到[{}]展示页面：id={}", SERVICE_NAME, id);

        ModelAndView mav = new ModelAndView(VIEW_PATH + "detail.html");

        mav.put("bean", this.operateLogService.getById(id));

        return mav;
    }

    @Mapping(path = "", method = MethodType.GET)
    @SaCheckLogin
    public ModelAndView index(Context ctx) {
        this.log.info("跳转到[{}]主页面", SERVICE_NAME);

        ModelAndView mav = new ModelAndView(VIEW_PATH + "index.html");

        mav.putAll(ctx.paramMap());

        return mav;
    }

    /**
     * 分页查询功能
     */
    @Mapping(path = "query", method = MethodType.POST)
    @SaCheckPermission(value = {"monitor:operate:query"})
    public LayuiPage<SysOperateLog> query(Context ctx, @NotNull OperateLogQueryObject query) {
        this.log.info("执行[{}]分页查询：query={}", SERVICE_NAME, query);

        query.setSortField(CharSequenceUtil.toUnderlineCase(query.getSortField()));

        /*
         * 服务调用
         */
        Page<SysOperateLog> result =
            this.operateLogService.pageByMap(new Page<>(query.getPageNumber(), query.getPageSize()), query.toMap());

        /*
         * 返回值处理
         */
        return new LayuiPage<>(result.getRecords(), result.getTotalRow());
    }

}
